﻿/******************************************************************************
 *      Class:      EnumStringFixConverter
 *      Authors:    Michael Slezak, Samuel Harrington
 *      Implements: IValueConverter
 *      
 * ----------------------------------------------------------------------------
 * 
 *      Description:
 *          This is a converter used in XAML for converting between a string
 *          and its enumerated representation with the proper enumeration. It
 *          also reformats the string to be more presentable using regular
 *          expressions.
 *          
 ******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
//   INCLUDES
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Text.RegularExpressions;

namespace WeatherStationProgram
{
    public class EnumStringFixConverter : IValueConverter
    {
       /***********************************************************************
       *      Function: Convert
       *      Authors:  Michael Slezak, Samuel Harrington
       *      Parameters:
       *          object value - the object to convert
       *          Type targetType - the type to convert to
       *          object parameter - not sure what this is and is not relevant to the function
       *          CultureInfo culture - same as parameter. Irrelevant to the function
       *      Returns:  object
       *      Purpose:  Converts an enumerated value to a string.
       * *********************************************************************/
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Regex regex = new Regex(@"([a-z][a-z]+)|([A-Z][a-z]+)");
            string result = string.Empty;
            bool notfirst = false; // keeps from having an extra space at the end

            foreach (Match match in regex.Matches(value.ToString()))
            {
                if (notfirst)
                    result += " ";
                result += match.Value;
                notfirst = true;
            }

            result = result.Insert(0, Char.ToUpper(result[0]).ToString());
            result = result.Remove(1, 1);

            return result;
        }

      /***********************************************************************
       *      Function: ConvertBack
       *      Authors:  Michael Slezak, Samuel Harrington
       *      Parameters:
       *          object value - the object to convert
       *          Type targetType - the type to convert to
       *          object parameter - not sure what this is and is not relevant to the function
       *          CultureInfo culture - same as parameter. Irrelevant to the function
       *      Returns:  object
       *      Purpose:  Converts a string to the proper enumerated value.
       * *********************************************************************/
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Enum.Parse(targetType, value.ToString());
        }
    }
}
